﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace blackhouse.BlackApart.Data {
    public abstract class AdoMailTemplateDataAccess : IMailTemplateDataAccess {

        #region IMailTemplateDataAccess Members

		public MailTemplateData GetMailTemplate(int templateId, string language) {
            return this.ReadMailTemplate(this.GetReaderForMailTemplate(templateId, language));
        }

        public Dictionary<int, int> GetReservationStatusTemplates() {
            return this.ReadReservationStatusTemplates(this.GetReaderForReservationStatusTemplates());
        }

        #endregion

        #region Abstracts

		public abstract IDataReader GetReaderForMailTemplate(int templateId, string language);

        public abstract IDataReader GetReaderForReservationStatusTemplates();

        #endregion

        #region Methods

        private Dictionary<int, int> ReadReservationStatusTemplates(IDataReader reader) {
            Dictionary<int, int> data = new Dictionary<int, int>();
            while (reader.Read()) {
                this.ReadReservationStatusTemplatesLine(reader, data);
            }
            reader.Close();
            return data;
        }

        private void ReadReservationStatusTemplatesLine(IDataReader reader, Dictionary<int, int> appendToData) {
            appendToData.Add(Convert.ToInt32(reader["ReservationStatus"]), Convert.ToInt32(reader["TemplateId"]));
        }

        private MailTemplateData ReadMailTemplate(IDataReader reader) {
            if (reader.Read())
                return this.ReadMailTemplateFields(reader);
            else return default(MailTemplateData);
        }

        private MailTemplateData ReadMailTemplateFields(IDataReader reader) {
            MailTemplateData data = new MailTemplateData();
            data.Id = Convert.ToInt32(reader["Id"]);
            data.LastUpdateTime = Convert.ToDateTime(reader["LastUpdateTime"]);
            data.TemplateContent = Convert.ToString(reader["TemplateContent"]);
            data.Title = Convert.ToString(reader["Title"]);
            return data;
        }

        #endregion

    }
}
